package com.hzhr.iot.cqgas.ds.controller;

import com.hzhr.iot.core.util.HzhrDateUtil;
import com.hzhr.iot.core.util.HzhrTypeUtil;
import com.hzhr.iot.cqgas.ds.dao.device.custom.DeviceDao;
import com.hzhr.iot.cqgas.ds.dao.device.example.DevCollectDeviceDao;
import com.hzhr.iot.cqgas.ds.dao.device.example.DevRegulatorCollectDataDao;
import com.hzhr.iot.cqgas.ds.entity.device.DevCollectDeviceEntity;
import com.hzhr.iot.cqgas.ds.entity.device.DevCollectDeviceExample;
import com.hzhr.iot.cqgas.ds.entity.device.DevRegulatorCollectDataEntity;
import com.hzhr.iot.cqgas.ds.service.CollectDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
public class RegulatorCollectDataController {
    @Autowired
    DeviceDao deviceDao;
    @Autowired
    DevRegulatorCollectDataDao devRegulatorCollectDataDao;

    @RequestMapping(value = "/syncRegulatorCollectData")
    public void syncRegulatorCollectData() {
        List<Map<String, Object>> collectDatas = deviceDao.select12();
        for (Map<String, Object> collectData :
        collectDatas) {
            Integer iotDeviceId = HzhrTypeUtil.object2integer(collectData.get("IOT_DEVICE_ID"));
            String CURRENT_TM = HzhrTypeUtil.object2string(collectData.get("CURRENT_TM"));
            if (iotDeviceId != null && !StringUtils.isEmpty(CURRENT_TM)) {
                int count = deviceDao.select13(iotDeviceId, CURRENT_TM);
                if (count == 0) {
                    Integer COLLECT_DEVICE_ID = HzhrTypeUtil.object2integer(collectData.get("COLLECT_DEVICE_ID"));

                    Double PRESSURE = HzhrTypeUtil.object2double(collectData.get("PRESSURE"));
                    Float TEMPERATURE = HzhrTypeUtil.object2float(collectData.get("TEMPERATURE"));

                    DevRegulatorCollectDataEntity devRegulatorCollectDataEntity = new DevRegulatorCollectDataEntity();
                    devRegulatorCollectDataEntity.setIotDeviceId(iotDeviceId);
                    devRegulatorCollectDataEntity.setCollectDeviceId(COLLECT_DEVICE_ID);
                    devRegulatorCollectDataEntity.setPressure(PRESSURE);
                    devRegulatorCollectDataEntity.setEnvTemperature(TEMPERATURE);
                    devRegulatorCollectDataEntity.setCollectTime(HzhrDateUtil.strToDate(CURRENT_TM));
                    devRegulatorCollectDataEntity.setCreateTime(new Date());
                    devRegulatorCollectDataEntity.setIsValid("1");
                    devRegulatorCollectDataEntity.setState("1");
                    devRegulatorCollectDataDao.insert(devRegulatorCollectDataEntity);
                }
            }
        }
    }
}
